package com.osgc.rf.unit.base;

import java.sql.Connection;

import com.inca.np.auth.Userruninfo;
import com.osgc.rf.np.url.dao.DataConnect;
import com.osgc.rf.np.url.dao.Navigation;
import com.osgc.rf.np.url.dao.User;
import com.osgc.rf.np.url.util.Base;
import com.osgc.rf.unit.dao.ResultInfo;

public abstract class TaskModel extends Base {

	protected Navigation navigation = null;
	protected User user;
	protected Userruninfo userinfo;

	public void setInfoFileVm(String vm) {
		sessionHelper.putContext("infofilevm", vm);
	}



	abstract protected String getTemp();

	abstract protected String query(Connection con) throws Exception;
	abstract protected String requery(Connection con) throws Exception;
	abstract protected ResultInfo excute(Connection con) throws Exception ;
	abstract public void show();
	@Override
	public void beforeInvoke() throws Exception {
		navigation = (Navigation) sessionHelper.getContext("navigation");
		user = (User) sessionHelper.getContext("user");
		if (user != null) {
			userinfo = new Userruninfo();
			userinfo.setUserid(user.getUserid());
			userinfo.setUsername(user.getUsername());
			if (navigation == null) {
				navigation = new Navigation();
				sessionHelper.putContext("navigation", navigation);
			}
			navigation.setMenu(user.getOp());
		}
		setRedirect(getTemp());
	}

	@Override
	public void afterInvoke() throws Exception {

	}
	public void index() {
		navigation.setId("");
		sessionHelper.remove();
		setInfoFileVm("scan");
	}
	public void query() {		
		Connection con = null;
		try {
			con = DataConnect.getConnect();
			String barcode = query(con);
			navigation.setId(barcode);
			con.commit();
		} catch (Exception e) {
			logger.error(e.getMessage());
			DataConnect.rollback(con);
		} finally {
			DataConnect.close(con);
		}		
	}	
	public void excute() {
		Connection con = null;
		try {
			con = DataConnect.getConnect();
			ResultInfo result = excute(con);
			sessionHelper.putContext("result", result);
			con.commit();
		} catch (Exception e) {
			DataConnect.rollback(con);
			logger.error(e.getMessage());
			ResultInfo	result = ResultInfo.getErrorResult(e);
			sessionHelper.putContext("result", result);
		} finally {
			DataConnect.close(con);
		}
		setRedirect("default/json");
	}

	
}
